package com.gonsin.ta.logic.utils;

import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.MalformedJsonException;

import java.io.IOException;

public class TaJson {

    /**
     * 使用gson序列化
     */
    public static String serialize(Object context) {
        return new GsonBuilder().create().toJson(context);
//        return new GsonBuilder().setNumberToNumberStrategy(DEFAULT_STRATEGY).create().toJson(context);
    }

    /**
     * 使用gson序列化
     */
    public static <T> T deSerialize(String json, Class<T> clazz) {
        return new GsonBuilder().create().fromJson(json, clazz);
//        return new GsonBuilder().setNumberToNumberStrategy(DEFAULT_STRATEGY).create().fromJson(json, clazz);
    }

//    private static LongOrDouble DEFAULT_STRATEGY = new LongOrDouble();

//    static class LongOrDouble implements ToNumberStrategy{
//
//        @Override
//        public Number readNumber(JsonReader in) throws IOException {
//            String value = in.nextString();
//            try {
//                return Long.parseLong(value);
//            } catch (NumberFormatException longE) {
//                try {
//                    Double d = Double.valueOf(value);
//                    if ((d.isInfinite() || d.isNaN()) && !in.isLenient()) {
//                        throw new MalformedJsonException("JSON forbids NaN and infinities: " + d + "; at path " + in.getPreviousPath());
//                    }
//                    return d;
//                } catch (NumberFormatException doubleE) {
//                    throw new JsonParseException("Cannot parse " + value + "; at path " + in.getPreviousPath(), doubleE);
//                }
//            }
//        }
//    }

}
